package com.qf.mvc2203.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/up")
public class UploadController {
    @Value("${uploadpath}")
    private String fileUploadPath;//获取配置文件中的文件上传路径

    @RequestMapping("/test1")
    public String test1(MultipartFile file, String username) throws IOException {
        System.out.println(username);
        //String filepath = "E:\\JavaProject\\Idea\\File\\photo\\"+username+".jpg";
        File file1 = createFile(file);//产生一个用于保存上传文件的唯一的文件路径
        file.transferTo(file1);
        return "ok";
    }

    private File createFile(MultipartFile file) {
        //最终的文件名，由前缀（唯一）+原始文件名
        String prefix = UUID.randomUUID().toString();
        String originalFilename = file.getOriginalFilename();//原始上传文件的文件名
        String fileName = prefix + originalFilename;
        //如何处理文件存放的路径？
        String finaFilepath = fileUploadPath + fileName;
        File file1 = new File(finaFilepath);
        return file1;
    }
}
